//
//  LiveCollectionCell.m
//  UIA段项目 墨家天气
//
//  Created by dllo on 15/8/25.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "LiveCollectionCell.h"
#import "SmallLiveCollectiontViewCell.h"
#import "URL.h"
#import "UIImageView+WebCache.h"
#import "DetailViewController.h"
@implementation LiveCollectionCell
-(void)dealloc
{
    [_smallCollect release];
    [_sc release];
    [_arr release];
    [_arr2 release];
    [_imageArr release];
    [_senToArr release];
    [_acticity release];

    [super dealloc];
  
}
- (instancetype)initWithFrame:(CGRect)frame
{  
    
    
    
    self = [super initWithFrame:frame];
    if (self) {
        self.senToArr = [NSMutableArray array];
        self.imageArr = [NSMutableArray array];
        self.arr = [NSMutableArray array];
        
        
        
        //sc 初始化
        self.sc = [[UIScrollView alloc]init];
        
        [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
    }
    return self;
}

- (void)sendValueForDic:(NSMutableArray *)sendArr temp:(NSInteger)temp
{
    
    
    NSArray *arr = self.contentView.subviews;
    for (UIView *view in  arr) {
        [view removeFromSuperview];
    }
    self.senToArr = sendArr;
    self.temp = temp;
    
   
   if (temp == 0) {
        
  

       UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
       flow.itemSize = CGSizeMake(self.contentView.frame.size.width / 3, 120 * OffHeight);
       [flow setMinimumInteritemSpacing:0];
       [flow setMinimumLineSpacing:0];
       self.smallCollect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 202 * OffHeight, self.contentView.frame.size.width, self.contentView.frame.size.height - 250 * OffHeight) collectionViewLayout:flow];
       self.smallCollect.delegate = self;
       self.smallCollect.dataSource = self;
       [self.contentView addSubview:_smallCollect];
       
       [self.smallCollect registerClass:[SmallLiveCollectiontViewCell class] forCellWithReuseIdentifier:@"dfds"];
       
       
        self.sc.frame =  CGRectMake(0, 0, self.contentView.frame.size.width, 200 *OffHeight);
       _sc.delegate = self;
       _sc.contentSize = CGSizeMake(self.imageArr.count * self.contentView.frame.size.width, 0);
       _sc.pagingEnabled = YES;
       [self.contentView addSubview:_sc];
     
       
  
       
       
       
         
         
  
    }else{
        
      
        
        UICollectionViewFlowLayout *flow = [[UICollectionViewFlowLayout alloc]init];
        [flow setMinimumLineSpacing:0];
        flow.itemSize = CGSizeMake(self.contentView.frame.size.width,150 * OffHeight);
        self.smallCollect = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 52 * OffHeight, self.contentView.frame.size.width, self.contentView.frame.size.height - 100 * OffHeight) collectionViewLayout:flow];

        self.smallCollect.delegate = self;
        self.smallCollect.dataSource = self;
        [self.contentView addSubview:_smallCollect];
        [self.smallCollect registerClass:[SmallLiveCollectiontViewCell class] forCellWithReuseIdentifier:@"dfds"];
        [_smallCollect release];
        [flow release];

    }
    self.smallCollect.showsVerticalScrollIndicator = NO;
    self.smallCollect.backgroundColor = [UIColor clearColor];
    [self addGetHandle];
    
    
}
- (void)timerAction:(NSTimer *)timer
{
    
    if (_sc.contentOffset.x == self.contentView.frame.size.width * (self.imageArr.count - 1) ) {
      _sc.contentOffset = CGPointMake(self.contentView.frame.size.width, 0);
 
    }else
    {
        
        [UIView animateWithDuration:0.5 animations:^{
            
            _sc.contentOffset = CGPointMake(_sc.contentOffset.x + self.contentView.frame.size.width, 0);
            
        } completion:^(BOOL finished) {
            
        }];
        

    }
}
- (void)addGetHandle

{


    NSString *string = [[self.senToArr objectAtIndex:2] objectForKey:@"feilunbo"];
  
    [URL conectionWithUrl:string dic:[self.senToArr objectAtIndex:0] block:^(NSData *data) {
       
        if (self.temp == 0) {
         
            NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSUTF8StringEncoding error:nil];
            NSArray * myTitle = [dic objectForKey:@"statuses"];
            
            [self.arr addObjectsFromArray:myTitle];
            
        }else{
        
            self.arr= [NSJSONSerialization JSONObjectWithData:data options:NSUTF8StringEncoding error:nil];

       
        }
        [self.smallCollect reloadData];
  
        
    }];
    
   
        
   
    
    NSString *string2 = [[self.senToArr objectAtIndex:2] objectForKey:@"lunbo"];
 
    [URL conectionWithUrl:string2 dic:[self.senToArr objectAtIndex:1] block:^(NSData *data) {
        NSMutableDictionary *reciveDic = [NSJSONSerialization JSONObjectWithData:data options:NSUTF8StringEncoding error:nil];
        
        self.imageArr = [[reciveDic objectForKey:@"data"] objectForKey:@"banners"];
        
        [self addLunBoPic];
        
    }];
    
}



- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
  
    
    SmallLiveCollectiontViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"dfds" forIndexPath:indexPath];
      if (self.senToArr.count != 0) {
          
          
          
          if (self.temp == 0) {
              
              
              cell.lable.text =  [[[[[self.arr objectAtIndex:indexPath.item] objectForKey:@"annotations"] objectAtIndex:0] objectForKey:@"place"]objectForKey:@"title"];
              
              [cell.imageV sd_setImageWithURL:[[[[self.arr objectAtIndex:indexPath.item]objectForKey:@"pic_urls"]objectAtIndex:0]objectForKey:@"thumbnail_pic" ] placeholderImage:[UIImage imageNamed:@"zanshi.jpg"]];
          }else{
              
              
              cell.lable.text =  [[self.arr objectAtIndex:indexPath.item] objectForKey:@"title"];
              
              [cell.imageV sd_setImageWithURL:[[self.arr objectAtIndex:indexPath.item]objectForKey:@"image"] placeholderImage:[UIImage imageNamed:@"zanshi.jpg"]] ;
              
              
          }
          
          
      }
    
    [cell sendTEMP:self.temp];
    return cell;
    
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
      return self.arr.count;
    
}



- (void)addLunBoPic
{  //移除所有子视图  重新铺  因为每次解析之后都会创建以下内容
  
    
    if (self.imageArr.count != 0) {
        
    NSArray *arr = self.sc.subviews;
    for (UIView *view in  arr) {
        [view removeFromSuperview];
    }
    
    for (int i = 0;  i < self.imageArr.count;  i++) {
        UIImageView *imageV = [[UIImageView
                                alloc]initWithFrame:CGRectMake(i * self.contentView.frame.size.width,0 ,self.contentView.frame.size.width , 240 * OffHeight)];
        
        
        [imageV sd_setImageWithURL:[[self.imageArr objectAtIndex:i]objectForKey:@"image_url"]];
        
        UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(i * self.contentView.frame.size.width,170 * OffHeight,self.contentView.frame.size.width ,30 * OffHeight)];
        title.text  = [[[self.imageArr objectAtIndex:i]objectForKey:@"target" ]objectForKey:@"subtitle"];
        title.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.5];
        title.textColor = [UIColor whiteColor];
        
        [_sc addSubview:imageV];
        [_sc addSubview:title];
        [title release];
        [imageV release];
    }
  
    }

}



-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
  
   [self.daili GotoDetailWithArr:self.arr temp:self.temp index:indexPath.item ];
      
    
    
   
}

@end
